# -*- Mode: Python -*- vi:si:et:sw=4:sts=4:ts=4:syntax=python
import shutil


class Recipe(recipe.Recipe):
    name = 'mingw-runtime'
    version = '0.1'
    licenses = [License.LGPL]
    btype = BuildType.CUSTOM
    stype = SourceType.CUSTOM
    runtime_dep = True

    files_libs = ['libstdc++', 'libwinpthread', 'libgomp', 'libgcc_s_sjlj']
    files_satic = []
    files_headers = ['include/GL']

    def prepare(self):
        if self.config.target_platform != Platform.WINDOWS:
            raise InvalidRecipeError(self, "Invalid platform")
        # mingw's static libraries
        self.mingw_static_files = ['%(host)s/lib/libmingwex.a',
                                   '%(host)s/lib/libdxerr9.a',
                                   '%(host)s/lib/libmoldname.a']
        if self.config.platform == Platform.WINDOWS:
            self.mingw_static_files += ['%(mingw)s/lib/gcc/%(host)s/4.7.3/libgcc.a']
        # static build deps
        if self.config.platform == Platform.WINDOWS:
            # the native compiler install libraries in 'lib' and
            # not in the cross prefix
            mingw = '.'
        else:
            mingw = self.config.host
        self.mingw_static_files = [f % {'mingw':mingw, 'host':self.config.host} for f in self.mingw_static_files]
        self.files_static = [os.path.join('lib', os.path.basename(f)) for f in self.mingw_static_files]

    def install(self):
        # Copy libstdc++ to the prefix and update the .la files with the
        # the prefix path.
        if self.config.platform == Platform.WINDOWS:
            # the native compiler install dll's in 'bin' and
            # not in the cross prefix
            binmingw = 'bin'
            libmingw = 'lib'
        else:
            binmingw = os.path.join(self.config.host, 'lib')
            libmingw = os.path.join(self.config.host, 'lib')
        # copy the dll
        if not os.path.exists(os.path.join(self.config.prefix, 'bin')):
            os.makedirs(os.path.join(self.config.prefix, 'bin'))
        if not os.path.exists(os.path.join(self.config.prefix, 'lib')):
            os.makedirs(os.path.join(self.config.prefix, 'lib'))
        for f in ['libwinpthread-1']:
            shutil.copy(
                os.path.join(self.config.toolchain_prefix, 'bin', f + '.dll'),
                os.path.join(self.config.prefix, 'bin', f + '.dll'))
        for f in ['libgomp-1', 'libstdc++-6']:
            shutil.copy(
                os.path.join(self.config.toolchain_prefix, binmingw, f + '.dll'),
                os.path.join(self.config.prefix, 'bin', f + '.dll'))
        for f in ['libgcc_s_sjlj-1']:
            shutil.copy(
                os.path.join(self.config.toolchain_prefix, libmingw, f + '.dll'),
                os.path.join(self.config.prefix, 'bin', f + '.dll'))
        # copy the development libraries
        for f in ['libstdc++', 'libgomp']:
            # Copy the dll.a
            shutil.copy(
                os.path.join(self.config.toolchain_prefix, libmingw, f + '.dll.a'),
                os.path.join(self.config.prefix, 'lib', f + '.dll.a'))
            # Copy and update the .la
            src = os.path.join(self.config.toolchain_prefix, libmingw, f + '.la')
            dest = os.path.join(self.config.prefix, 'lib', f + '.la')
            if os.path.exists(dest):
                os.remove(dest)
            with open(src, 'r') as f:
                content = f.readlines()[:-1]
                content.append("libdir='%s'" % os.path.join(self.config.prefix, 'lib'))
                with open(dest, 'w+') as d:
                    d.writelines(content)
        # copy winpthreads
        for f in ['libpthread', 'libwinpthread']:
            shutil.copy(os.path.join(self.config.toolchain_prefix, 'lib', f + '.dll.a'),
                os.path.join(self.config.prefix, 'lib', f + '.dll.a'))
            shutil.copy(os.path.join(self.config.toolchain_prefix, 'lib', f + '.a'),
                os.path.join(self.config.prefix, 'lib', f + '.a'))
        src = os.path.join(self.config.toolchain_prefix, 'lib', 'libwinpthread' + '.la')
        dest = os.path.join(self.config.prefix, 'lib', f + '.la')
        if os.path.exists(dest):
            os.remove(dest)
        with open(src, 'r') as f:
            content = f.readlines()[:-1]
            content.append("libdir='%s'" % os.path.join(self.config.prefix, 'lib'))
            with open(dest, 'w+') as d:
                d.writelines(content)
        # install headers and static libraries
        for f in self.files_headers:
            src = os.path.join(self.config.toolchain_prefix, self.config.host, f)
            dest = os.path.join(self.config.prefix, f)
            if os.path.exists(dest):
                shutil.rmtree(dest)
            shutil.copytree(src, dest)

        for f in self.mingw_static_files:
            out_file = os.path.join('lib', os.path.basename(f))
            self.files_static.append(out_file)
            shutil.copy(os.path.join(self.config.toolchain_prefix, f),
                os.path.join(self.config.prefix, out_file))
